package com.example.administrator.zhongyichou.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.Bean.PayServiceBean;
import com.example.administrator.zhongyichou.R;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.config.Constants;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.utils.WebServiceUtils;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.ips.ips2wxassist.assist.IPS2WXAssist;
import com.ips.ips2wxassist.entity.WXRequest;
import com.switfpass.pay.MainApplication;

import org.ksoap2.serialization.SoapObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/12/11.
 * 描述：充值
 */

public class RechargeActivity extends BaseActivity {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.edit_money)
    EditText editMoney;
    @BindView(R.id.btn_recharge)
    Button btnRecharge;
    private String memberId;
    private String price;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
        memberId = getIntent().getStringExtra("member_id");
    }

    @Override
    protected void responseData(GsonBaseProtocol mGsonBaseProtocal) {

    }

    @Override
    protected HttpLoader initHttpLoader() {
        return null;
    }

    @Override
    protected int initContentView() {
        return R.layout.recharge_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {
        return false;
    }

    @OnClick(R.id.btn_recharge)
    public void onViewClicked() {
        pay();
    }


    /**
     * 后台支付接口
     */
    private void pay() {
        price = editMoney.getText().toString().trim();
        if (TextUtils.isEmpty(price)) {
            ToastUtil.showToast("请输入金额");
            return;
        }
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(PayServiceBean.class);
        Map map = new HashMap();
        map.put("user_id", MyApplication.getUserID());
        map.put("member_id", memberId);
        map.put("recharge_money", price + "");
        map.put("order_type", "1");
        map.put("trade_type", "APP");
        requestHttpLoader.setParams(NetConfig.PAY_MUTUAL, map);
        requestHttpLoader.executor(new ResultListener<PayServiceBean>() {
            @Override
            public void success(PayServiceBean mGsonBaseProtocol) {
                WebServiceUtils.callWebService("https://thumbpay.e-years.com/psfp-webscan/services/scan?wsdl", "scanPay", mGsonBaseProtocol, new WebServiceUtils.WebServiceCallBack() {
                    @Override
                    public void callBack(SoapObject result) {

                    }
                });
//                HX_Pay();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }

    /**
     * 环迅微信支付
     */
    private void HX_Pay() {
//        WXRequest request = new WXRequest();
//        request.setPayInfo("{\"PayInfo\":{\"tokenid\":\"\",\"partnerid\":\"19000000109\",\"prepayid\":\"WX1217752501201407033233368018\",\"package\":\"Sign=WXPay\",\"noncestr\":\"5K8264ILTKCH16CQ2502SI8ZNMTM67VS\",\"timestamp\":\"1412000000\",\"sign\":\"C380BEC2BFD727A4B6845133519F3AD6\"}}");//微信支付信息
//        request.setAmount(price); //金额
//        request.setTradeType(MainApplication.WX_APP_TYPE); //app支付类型
//        request.setAppId(Constants.APP_ID);//appid为商户自己在微信开放平台的应用appid
//        new IPS2WXAssist(this).startPay(request);//调起插件
    }


    private void initHead() {
        headerview.setTitle("充值");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }
}
